package com.taxi.comm.util;

public class BytesUtils {

	public static int byteArrayToInt(byte[] bytes) {

		int v = bytes[0] & 0xFF;
		v |= bytes[1] << 8 & 0xFF00;
		v |= bytes[2] << 16 & 0xFF0000;
		v |= bytes[3] << 24 & 0xFF000000;

		return v;

	}

	public static byte[] intToByteArray(int i) {

		byte[] bytes = new byte[4];
		bytes[3] = (byte) ((i >> 24) & 0xFF);
		bytes[2] = (byte) ((i >> 16) & 0xFF);
		bytes[1] = (byte) ((i >> 8) & 0xFF);
		bytes[0] = (byte) (i & 0xFF);
		
		return bytes;

	}
	
	public static String bytes2HexString(byte[] b)
	{
		String ret = "";
		for (int i = 0; i < b.length; i++)
		{
			String hex = Integer.toHexString(b[i] & 0xFF);
			if (hex.length() == 1)
			{
				hex = '0' + hex;
			}
			ret += " 0x" + hex.toUpperCase();
		}
		return ret.length() > 1 ? ret.substring(1) : ret;
	}

}
